package com.iniapp.app.web.global;


import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.inibb.app.constant.GenericCodeConstant;
import com.inibb.app.global.I18nGenericCodeMapBean;
import com.inibb.app.global.I18nMessagesMapBean;
import com.inibb.app.service.admin.IGenericCodeService;
import com.inibb.app.service.admin.II18nMsgService;
import com.inibb.app.service.admin.ISaSystemParamService;
import com.inibb.app.vo.GenericCodeVO;
import com.inibb.app.vo.admin.SaSystemParamVO;

@Controller("systemConfigBean")
@Scope("application")//page
public class SystemConfigBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IGenericCodeService genericCodeService;
	@Autowired
	private ISaSystemParamService systemParamService;
	@Autowired
	private II18nMsgService i18nMsgService;
	
	private I18nMessagesMapBean i18nMessagesMap;
	private I18nGenericCodeMapBean i18nGenericCodeMap;
	private List<GenericCodeVO> codeTypes;
	private List<SaSystemParamVO> systemParams;
	private List<GenericCodeVO> productTypes;
	private List<GenericCodeVO> productUnits;

	@PostConstruct
	public void init(){
		load();
	}

	public void load(){		
		//sysConfigMap = new HashMap<String, Object>();
		i18nMessagesMap = new I18nMessagesMapBean();
		i18nMessagesMap.init(i18nMsgService);
		//i18n code map 
		i18nGenericCodeMap = new I18nGenericCodeMapBean();
		i18nGenericCodeMap.init(genericCodeService);
		
		codeTypes = genericCodeService.searchCodeType();
		
		//get system parameter
		systemParams = systemParamService.searchSystemParam();
		Map<String, SaSystemParamVO> systemParamMap = new HashMap<String, SaSystemParamVO>();
		for(SaSystemParamVO v:systemParams){
			systemParamMap.put(v.getName(), v);
		}
		
		//GET product type
		GenericCodeVO code = genericCodeService.searchCodeTypeByCode(GenericCodeConstant.CODE_TYPE_GOODS_TYPE);
		if(code!=null){
			productTypes = genericCodeService.searchCodeByParentId(code.getCodeId());
		}
		//GET product unit
		GenericCodeVO codeUnit = genericCodeService.searchCodeTypeByCode(GenericCodeConstant.CODE_TYPE_PRODUCT_UNIT);
		if(codeUnit!=null){
			productUnits = genericCodeService.searchCodeByParentId(codeUnit.getCodeId());
		}
	}

	public I18nMessagesMapBean getI18nMessagesMap() {
		return i18nMessagesMap;
	}

	public void setI18nMessagesMap(I18nMessagesMapBean i18nMessagesMap) {
		this.i18nMessagesMap = i18nMessagesMap;
	}

	public I18nGenericCodeMapBean getI18nGenericCodeMap() {
		return i18nGenericCodeMap;
	}

	public void setI18nGenericCodeMap(I18nGenericCodeMapBean i18nGenericCodeMap) {
		this.i18nGenericCodeMap = i18nGenericCodeMap;
	}

	public List<GenericCodeVO> getCodeTypes() {
		return codeTypes;
	}

	public void setCodeTypes(List<GenericCodeVO> codeTypes) {
		this.codeTypes = codeTypes;
	}

	public List<SaSystemParamVO> getSystemParams() {
		return systemParams;
	}

	public void setSystemParams(List<SaSystemParamVO> systemParams) {
		this.systemParams = systemParams;
	}

	public List<GenericCodeVO> getProductTypes() {
		return productTypes;
	}

	public void setProductTypes(List<GenericCodeVO> productTypes) {
		this.productTypes = productTypes;
	}

	public List<GenericCodeVO> getProductUnits() {
		return productUnits;
	}

	public void setProductUnits(List<GenericCodeVO> productUnits) {
		this.productUnits = productUnits;
	}
	
	
}
